package com.apobates.forum.toro.test.core;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

public class BaseInitData {
    protected static HashMap<String,Members> mhm = new HashMap<>();
    protected static HashMap<String,Product> phm = new HashMap<>();
    protected static HashMap<Long,Orders> ohm = new HashMap<>();
    protected static HashMap<String, Set<Coupon>> coupones = new HashMap<>();
    protected static Orders o3;
    protected static Orders o4;
    protected static Orders o5;
    protected static Function<String,Members> memberSelectFun = (memberId) -> mhm.get(memberId);
    protected static Function<String,Product> productSelectFun = (productId) -> phm.get(productId);
    protected static Function<Long,Product> productTypeSelectFun = (productId) -> phm.get(productId+"");
    protected static Function<Long,Members> memberTypeSelectFun = (memberId) -> mhm.get(memberId+"");
    protected static Function<Long,Orders> orderSelectFun = (orderId) -> ohm.get(orderId);
    protected static Function<String,Set<Coupon>> orderCouponFun = (orderId) -> coupones.get(orderId);

    static {
        Members m1 = new Members();
        m1.setId(1);m1.setNames("xiao");
        mhm.put("1", m1);
        Members m2 = new Members();
        m2.setId(2);m2.setNames("xiaoya");
        mhm.put("2", m2);
        Members m3 = new Members();
        m3.setId(3);m3.setNames("xiaoku");
        mhm.put("3", m3);
        //
        Product p2 = new Product();
        p2.setId(100);p2.setPrice(10.00);p2.setTitle("java devp");
        phm.put("100", p2);
        Product p3 = new Product();
        p3.setId(101);p3.setPrice(100.00);p3.setTitle("spring devp");
        phm.put("101", p3);
        Product p4 = new Product();
        p4.setId(102);p4.setPrice(50.00);p4.setTitle("jpa devp");
        phm.put("102", p4);
        //
        o3 = new Orders();
        o3.setId(1000);o3.setAmout(100.00);o3.setNumber(10);o3.setMemberId(1);o3.setProductId(100);o3.setTimestamp(1001L);
        ohm.put(1000L, o3);
        o4 = new Orders();
        o4.setId(1001);o4.setAmout(500.00);o4.setNumber(5);o4.setMemberId(2);o4.setProductId(101);o4.setTimestamp(1003L);
        ohm.put(1001L, o4);
        o5= new Orders();
        o5.setId(1002);o5.setAmout(100.00);o5.setNumber(1);o5.setMemberId(2);o5.setProductId(101);o5.setTimestamp(1004L);
        ohm.put(1002L, o5);
        //
        Set<Coupon> tmp = new HashSet<>();
        tmp.add(new Coupon(1, "7折", 0.3, 1000));
        tmp.add(new Coupon(2, "满100减10", 0.1, 1000));
        coupones.put("1000", tmp);
    }


    public static Collection<Members> queryMemberFace(Collection<String> args) {
        return mhm.values().stream().filter(ele->args.contains(ele.getId()+"")).collect(Collectors.toSet());
    }

    public static Collection<Product> queryProductFace(Collection<String> args) {
        return phm.values().stream().filter(ele->args.contains(ele.getId()+"")).collect(Collectors.toSet());
    }
    public static Collection<Coupon> queryCouponFace(Collection<String> args) {
        return coupones.values().stream().flatMap(set->set.stream()).filter(ele->args.contains(ele.getOrderId()+"")).collect(Collectors.toSet());
    }
    public static Collection<Orders> queryOrdersById(Collection<Long> args) {
        return ohm.values().stream().filter(ele->args.contains(ele.getId())).collect(Collectors.toSet());
    }
    public static Collection<Orders> queryOrdersByBuyer(long buyer) {
        return ohm.values().stream().filter(ele->ele.getMemberId() == buyer).collect(Collectors.toSet());
    }
    public static Function<Collection<Long>, Collection<Orders>> queryOrdersFun = (Collection<Long> args) ->{
        return ohm.values().stream().filter(ele->args.contains(ele.getId())).collect(Collectors.toSet());
    };
    public static Collection<Members> queryMemberById(Collection<Long> args) {
        return mhm.values().stream().filter(ele->args.contains(ele.getId())).collect(Collectors.toSet());
    }

    public static Collection<Product> queryProductById(Collection<Long> args) {
        return phm.values().stream().filter(ele->args.contains(ele.getId())).collect(Collectors.toSet());
    }
}
